package pso;

public class BoundaryHandlerModulo implements BoundaryHandler {
	private Point lowerBound, upperBound;
	
	public BoundaryHandlerModulo(Point lowerBound, Point upperBound){
		this.lowerBound = lowerBound;
		this.upperBound = upperBound;
	}

	@Override
	public void applyBoundaryHandling(Particle p) {
		Point pos = p.getPosition();
		int dim = pos.getDimension();
		for(int i = 0; i < dim; i++){
			double x = pos.getCoordinate(i);
			double xlo = lowerBound.getCoordinate(i);
			double xhi = upperBound.getCoordinate(i);
			double len = xhi - xlo;
			while(x < xlo){
				x += len;
			}
			while(x > xhi){
				x -= len;
			}
			pos.setCoordinate(i, x);
		}
	}

}
